/**
 * 自定义函数
 * */


//列表
function getIndex(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        shop_id: uinfo.shop_id,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'sale/index',
        data: formData,
        success: function (res) {
            console.log(res);
            var h = '', tbody = $('tbody');
            if (res.code < 0) {
                h = '<tr class="text-c">';
                h += '<td colspan="10">没有记录</td>';
                h += '</tr>';
                tbody.html(h);
                return
            }
            tbody.html(getHtml(res.data));
            $('#pageTotal').html(res.total);
            pages.total = res.total;
            pageHtml(pages);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
//列表字符串
function getHtml(d){
    var h = '', i;
    h += '';
    for (i = 0; i < d.length; i ++) {
        h += '<tr class="text-c">';
        h += '<td>' + d[i].orderno + '</td>';
        h += '<td>' + d[i].plat_name + '</td>';
        h += '<td>' + d[i].shop_name + '</td>';
        h += '<td>' + d[i].intime + '</td>';
        h += '<td>' + d[i].outtime + '</td>';
        h += '<td>' + d[i].sale_num + '</td>';
        h += '<td>' + d[i].profit + '</td>';
        h += '<td>' + d[i].stock + '</td>';
        h += '<td style="color: red;">' + d[i].stock_base + '</td>';
        h += '<td class="td-manage">';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_edit(\'编辑\',\'sale-edit.html\', ' + d[i].id + ');">';
        h += '编辑';
        h += '<i class="Hui-iconfont">&#xe6df;</i>';
        h += '</a>';
        h += '&nbsp;&nbsp;';
        h += '<a href="javascript:;" class="ml-5" onclick="lay_del(' + d[i].id + ');" style="color: red;">';
        h += ' 删除';
        h += '<i class="Hui-iconfont"></i>';
        h += '</a>';
        h += '<input type="hidden" id="plat_type' + d[i].id +'" value="' + d[i].plat_type + '"/>';
        h += '<input type="hidden" id="intime' + d[i].id +'" value="' + d[i].intime + '"/>';
        h += '<input type="hidden" id="outtime' + d[i].id +'" value="' + d[i].outime + '"/>';
        h += '</td>';
        h += '</tr>';
    }
    return h;
}


//编辑弹窗
function lay_edit(title,url,id,w,h){
    url += '?id=' + id;
    //var tt = $('#title' + id).val();
    var plat_type = $('#plat_type' + id).val();
    var intime = $('#intime' + id).val();
    var outtime = $('#outime' + id).val();
    //tt = tt ? escape(tt) : '';
    plat_type = plat_type ? plat_type : 0;
    intime = intime ? intime : '';
    outtime = outtime ? outtime : '';
    //url += '&title=' + tt;
    url += '&plat_type=' + plat_type;
    url += '&intime=' + intime;
    url += '&outtime=' + outtime;
    layer_show(title,url,w,h);
}


//获取详情
function getOne(){
    if (!id) { return }
    $('#intime')[0].value = intime;
    $('#outtime')[0].value = outtime;
    var h = '';
    if (plat_type == 0) {
        h += '<option value="0" selected>-选择-</option>';
    } else {
        h += '<option value="0">-选择-</option>';
    }
    if (plat_type == 0) {
        h += '<option value="1" selected>美团</option>';
    } else {
        h += '<option value="1">美团</option>';
    }
    if (plat_type == 0) {
        h += '<option value="2" selected>饿了么</option>';
    } else {
        h += '<option value="2">饿了么</option>';
    }
    if (plat_type == 0) {
        h += '<option value="3" selected>京东</option>';
    } else {
        h += '<option value="3" selected>京东</option>';
    }
    $('#plat_type').html(h);
}


//显示图片
function showImg(e){
    var h = '';
    h += '<img src="' + e.img + '" style="width: 150px;"/>';
    h += '<input type="hidden" id="img" value="' + e.img + '"/>';
    $('#bigShow').html(h);
}


//修改
function modify(){
    var uinfo = getAdminInfo();
    if (!uinfo) { return }
    var formData = {
        id: id, shop_id: uinfo.shop_id,
        plat_type: $('#plat_type').val(),
        intime: $('#intime').val(),
        outtime: $('#outtime').val(),
    };
    if (formData.plat_type == '') {
        msgPop({msg: '平台必须的'}); return
    }
    if (formData.intime == '') {
        msgPop({msg: '入库时间必须的'}); return
    }
    if (formData.outtime == '') {
        msgPop({msg: '出库时间必须的'}); return
    }
    console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'sale/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '操作成功'});
            setTimeout(function(){
                top.location.reload();
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//删除
function lay_del(id) {
    var msg = '确认要 <b style="color: red; font-size: 20px;">删除</b> 吗？';
    layer.confirm(msg, function (index) {
        $.ajax({
            type: apiMethod,
            url: apiDomain + 'sale/del',
            data: {  id: id },
            success: function (res) {
                console.log(res);
                if (res.code < 0) {
                    msgPop({msg: res.msg});
                    return
                }
                layer.msg('已删除!', {icon: 1, time: 1000});
                setTimeout(function () {
                    top.location.reload();
                }, 500);
            },
            error: function () {
                console.log('错误了！');
            }
        });
    });
}